﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

// the HTML to PDF Converter namespace
using ExpertPdf.HtmlToPdf;

namespace WPF_HtmlConverterFeaturesDemo
{
    /// <summary>
    /// Interaction logic for MainForm.xaml
    /// </summary>
    public partial class MainForm : Window
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void btnConvert_Click(object sender, RoutedEventArgs e)
        {
            // create the HTML to PDF Converter object
            PdfConverter pdfConverter = new PdfConverter();

            // set the HTML to PDF converter options

            //set if the http links are enabled in the generated PDF
            pdfConverter.PdfDocumentOptions.LiveUrlsEnabled = (bool)cbLiveLinks.IsChecked;
            // set if the true type fonts are embedded in PDF
            pdfConverter.PdfDocumentOptions.EmbedFonts = (bool)cbEmbedFonts.IsChecked;
            // set if the images are compressed in PDF using JPEG
            pdfConverter.PdfDocumentOptions.JpegCompressionEnabled = (bool)cbJpegCompression.IsChecked;
            // set if JavaScript and ActiveX are enabled during conversion
            pdfConverter.ScriptsEnabled = (bool)cbJavaScriptEnabled.IsChecked;
            pdfConverter.ActiveXEnabled = (bool)cbActiveXEnabled.IsChecked;
            // bookmark H1 and H2 tags from HTML if bookmarking is enabled
            pdfConverter.PdfBookmarkOptions.TagNames = (bool)cbBookmarks.IsChecked ? new string[] { "H1", "H2" } : null;

            string outFile = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName), "RenderedPage.pdf");
            try
            {
                pdfConverter.SavePdfFromUrlToFile(textBoxWebPageURL.Text, outFile);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            MessageBoxResult dr = MessageBox.Show("Open the rendered file in an external viewer?", "Open Rendered File", MessageBoxButton.YesNo);
            if (dr == MessageBoxResult.Yes)
            {
                try
                {
                    System.Diagnostics.Process.Start(outFile);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
        }
    }
}
